package com.guli.mall.pms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.utils.QuickSort;
import com.guli.mall.pms.service.SkuImagesService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.pms.mapper.SpuImagesMapper;
import com.guli.mall.pms.entity.SpuImagesEntity;
import com.guli.mall.pms.service.SpuImagesService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesMapper, SpuImagesEntity> implements SpuImagesService {

    @Resource
    SkuImagesService skuImagesService;

    @Override
    public PageUtils queryPage(Integer currentPage,Integer size) {

        Page<SpuImagesEntity> page = new Page<>(currentPage,size);

        Page<SpuImagesEntity> resultPage = this.page(page);


        return new PageUtils(resultPage);
    }

    @Override
    public List<SpuImagesEntity> getBySpuIdAndAddImagePrefix(Long spuId) {
        QueryWrapper<SpuImagesEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("spu_id",spuId);

        List<SpuImagesEntity> list = this.list(wrapper);

        list.forEach(img->{
            img.setImgUrl(skuImagesService.getImagePrefix()+img.getImgUrl());
        });

        QuickSort.sort(list,(c1,c2)->{
            if(!ObjectUtils.isEmpty(c1.getImgSort())&&!ObjectUtils.isEmpty(c2.getImgSort())){
                return c1.getImgSort()-c2.getImgSort();
            }
            return 0;
        });

        return list;
    }

}